package com.gs.disk.modules.headImgAndWallper.service.admin;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gs.entity.GeneralPage;
import com.gs.common.exception.RenException;
import com.gs.common.util.ConvertUtils;
import com.gs.common.util.SQLUtils;
import com.gs.disk.modules.headImgAndWallper.dao.admin.AdminAPairResourceDao;
import com.gs.disk.modules.headImgAndWallper.dao.admin.AdminCategoryDao;
import com.gs.disk.modules.headImgAndWallper.dto.APairResourceDto;
import com.gs.disk.modules.headImgAndWallper.entity.APairResourceEntity;
import com.gs.disk.modules.headImgAndWallper.entity.CategoryEntity;
import com.gs.disk.service.common.OssService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class AdminAPairResourceService {

    @Resource
    private AdminAPairResourceDao dao;

    @Resource
    private AdminCategoryDao categoryDao;

    @Resource
    private OssService ossService;

    /**
     * 套图列表
     * @param categoryId 分类id
     * @param page 当前页
     * @param limit 数据量
     * @return
     */
    public GeneralPage<APairResourceDto> list(Long categoryId, Long page, Long limit) {
        QueryWrapper<APairResourceEntity> wrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(categoryId)) {
            wrapper.eq("category_id", categoryId);
        }

        GeneralPage generalPage = SQLUtils.toPageSql(dao, wrapper, page, limit);
        List<APairResourceDto> result = new LinkedList<>();
        generalPage.getList().forEach(r -> {
            APairResourceDto dto = ConvertUtils.sourceToTarget(r, APairResourceDto.class);
            Long backgroundImgId = dto.getBackgroundImgId();
            Long headImgId = dto.getHeadImgId();

            String backgroundImgURL = ossService.getDownloadURL(backgroundImgId);
            String headImgURL = ossService.getDownloadURL(headImgId);

            dto.setBackgroundImgIdUrl(backgroundImgURL);
            dto.setHeadImgUrl(headImgURL);
            result.add(dto);
        });

        generalPage.setList(result);

        return generalPage;
    }

    public void delete(Long id) {
        dao.deleteById(id);
    }

    /**
     * 上传套图资源
     * @param categoryId
     * @param headImgId
     * @param backgroundImgId
     * @param keyWord
     * @param nickname
     */
    public void uploadAPairResource(Long categoryId, Long headImgId, Long backgroundImgId, String keyWord, String nickname) {
        CategoryEntity categoryEntity = categoryDao.selectById(categoryId);
        if(ObjectUtil.isNull(categoryEntity) || ObjectUtil.isNull(categoryEntity.getId())) {
            throw new RenException("当前分类:”" + categoryEntity + "“不存在");
        }

        // 判断头像图片是否存在
        // 判断背景图片是否存在

        APairResourceEntity entity = new APairResourceEntity();
        entity.setCategoryName(categoryEntity.getCategoryName());
        entity.setCategoryId(categoryEntity.getId());
        entity.setHeadImgId(headImgId);
        entity.setBackgroundImgId(backgroundImgId);
        entity.setKeyword(keyWord);
        entity.setNickname(nickname);

        dao.insert(entity);

    }
}
